﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GEPSCodeTemplate
{
    public partial class GEPSCodeTemplateForm : Form
    {

        public GEPSCodeTemplateForm()
        {
            InitializeComponent();
            InitTemplateList();
        }

        public static void FindAllDir(string RootPath, Action <string, bool> action)
        {
            if (RootPath == "" || !Directory.Exists(RootPath))
            {
                Console.WriteLine("请输入正确的文件夹路径");
            }
            else
            {
                try
                {
                    DirectoryInfo di = new DirectoryInfo(RootPath);
                    FileInfo[] fis = di.GetFiles();
                    foreach (FileInfo fi in fis)
                    {
                        if (fi.Extension == ".EXT")
                        {
                            action(fi.FullName, false);
                        }
                    }
                    DirectoryInfo[] dis = di.GetDirectories();
                    foreach (DirectoryInfo diInfo in dis)
                    {
                        action(diInfo.FullName, true);
                        FindAllDir(diInfo.FullName, action);
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        private void InitNode(string Path, bool IsPath)
        {
            string strTemp = Path.Substring(Properties.Settings.Default.Contentpath.Length);
            strTemp = strTemp.Trim(new char[] { '\\', '/' });
            string[] arrNodePath = strTemp.Split(new char[] { '\\', '/' });
            TreeNode oNode = tvList.Nodes[0];
            foreach (string strNodeName in arrNodePath)
            {
                TreeNode[] oChildNodes = oNode.Nodes.Find(strNodeName, false);
                Debug.Assert(oChildNodes.Count() <= 1);
                if (oChildNodes.Count() == 0)
                {
                    oNode = oNode.Nodes.Add(strNodeName,strNodeName, IsPath?1:2);
                    oNode.Tag = Path;
                }
                else
                {
                    oNode = oChildNodes[0];
                }
            }
        }

        private void InitTemplateList()
        {
            string strRootPath = Properties.Settings.Default.Contentpath;
            while (string.IsNullOrEmpty(strRootPath))
            {
                MessageBox.Show("请先在设置中选择代码模板存储路径");
                OptionForm.Execute();
                strRootPath = Properties.Settings.Default.Contentpath;
            }
            tvList.Nodes[0].Nodes.Clear();
            FindAllDir(strRootPath, InitNode);
        }

        private void tvList_MouseClick(object sender, MouseEventArgs e)
        {
            TreeView oSender = sender as TreeView;
            Debug.Assert(oSender != null);
            oSender.SelectedNode = oSender.GetNodeAt(e.X, e.Y);
        }

        private void tbnOption_Click(object sender, EventArgs e)
        {
            OptionForm.Execute();
        }

        private void tvList_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            OpenTemplate(e.Node.Name, (string) e.Node.Tag);
        }

        private void OpenTemplate(string Caption, string FileName, bool EditMode = false)
        {
            string strCaption = Caption;
            if (strCaption.EndsWith(".EXT"))
            {
                TabPage oPage = new TabPage(strCaption);
                tcContent.TabPages.Add(oPage);
                ContentFrame oContentFrame = new ContentFrame(FileName, EditMode);
                oContentFrame.Parent = oPage;
                oContentFrame.Dock = DockStyle.Fill;
            }
        }

        private void miAddFolder_Click(object sender, EventArgs e)
        {
            if ((tvList.SelectedNode == null) || (tvList.SelectedNode.Name.EndsWith(".EXT")))
            {
                MessageBox.Show("请先选择目录节点");
                return;
            }
            string strContent = InputDialog.ShowInputDialog("目录名称");
            if (!String.IsNullOrEmpty(strContent))
            {
                if (tvList.Nodes.Find(strContent, false).Count() == 0)
                {
                    string strPath = Path.Combine((string) tvList.SelectedNode.Tag, strContent);
                    if (!Directory.Exists(strPath))
                    {
                        Directory.CreateDirectory(strPath);
                    }
                    tvList.SelectedNode.Nodes.Add(strContent, strContent, 1);
                }
            }
        }

        private void miAdd_Click(object sender, EventArgs e)
        {
            if ((tvList.SelectedNode == null) || (tvList.SelectedNode.Name.EndsWith(".EXT")))
            {
                MessageBox.Show("请先选择目录节点");
                return;
            }
            string strContent = InputDialog.ShowInputDialog("模板名称");
            if (!String.IsNullOrEmpty(strContent))
            {
                string strPath = "";
                strContent = (strContent + ".EXT").ToUpper();
                if (tvList.Nodes.Find(strContent, false).Count() == 0)
                {
                    strPath = Path.Combine((string)tvList.SelectedNode.Tag, strContent);
                    if (!File.Exists(strPath))
                    {
                        FileStream fs = null;
                        try
                        {
                            fs = File.Create(strPath);
                        }
                        finally
                        {
                            fs.Close();
                        }
                    }
                    TreeNode oNode = tvList.SelectedNode.Nodes.Add(strContent, strContent, 2);
                    oNode.Tag = strPath;
                }
                OpenTemplate(strContent, strPath, true);
            }
        }

        private void miEdit_Click(object sender, EventArgs e)
        {
            if ((tvList.SelectedNode == null) || (!tvList.SelectedNode.Name.EndsWith(".EXT")))
            {
                MessageBox.Show("请先选择模板节点");
                return;
            }
            OpenTemplate(tvList.SelectedNode.Name, (string)tvList.SelectedNode.Tag, true);
        }

        private void miDelete_Click(object sender, EventArgs e)
        {
            if ((tvList.SelectedNode == null) || (!tvList.SelectedNode.Name.EndsWith(".EXT")))
            {
                MessageBox.Show("请先选择模板节点");
                return;
            }
            File.Delete((string)tvList.SelectedNode.Tag);
            tvList.SelectedNode.Parent.Nodes.Remove(tvList.SelectedNode);
        }

        private void miDeleteFolder_Click(object sender, EventArgs e)
        {
            if ((tvList.SelectedNode == null) || (tvList.SelectedNode.Name.EndsWith(".EXT")))
            {
                MessageBox.Show("请先选择目录节点");
                return;
            }
            Directory.Delete((string)tvList.SelectedNode.Tag);
            tvList.SelectedNode.Parent.Nodes.Remove(tvList.SelectedNode);
        }

        private void miClose_Click(object sender, EventArgs e)
        {
            tcContent.TabPages.Remove(tcContent.SelectedTab);
        }

        private void miCloseOther_Click(object sender, EventArgs e)
        {
            for (int i = tcContent.TabCount - 1; i > tcContent.SelectedIndex; i--)
                tcContent.TabPages.RemoveAt(i);
            while (tcContent.TabCount != 1)
                tcContent.TabPages.RemoveAt(0);
        }

        private void miCloseAll_Click(object sender, EventArgs e)
        {
            while (tcContent.TabCount > 0)
                tcContent.TabPages.RemoveAt(0);
        }
    }
}
